﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TeachMe.Application.Controls;
using TeachMe.Application.Properties;

namespace TeachMe.Application.Forms.Options
{
    public partial class Hotkeys : OptionsControl
    {
        private readonly Item<HotkeyEdit, HotkeyType>[] _hotkeys;
        private List<Pair<ErrorProvider, bool>> _providers = new List<Pair<ErrorProvider, bool>>();

        public Hotkeys()
        {
            InitializeComponent();

            _hotkeys = new[] {
                new Item<HotkeyEdit, HotkeyType>(heCreateEntryHotkey, HotkeyType.CreateEntry), 
                new Item<HotkeyEdit, HotkeyType>(heDisplayEntryHotkey, HotkeyType.DisplayEntry),
                new Item<HotkeyEdit, HotkeyType>(heSuspendHotkey, HotkeyType.Suspend),
                new Item<HotkeyEdit, HotkeyType>(heTurboMode, HotkeyType.TurboMode),
                new Item<HotkeyEdit, HotkeyType>(heCloseHotkey, HotkeyType.Exit)
            };
        }

        private void HotkeyChanged(object sender, EventArgs e)
        {
            CheckHotkeys();
        }

        internal override void LoadOptions()
        {
            var usedHotkeys = from item in _hotkeys where Root.HotkeysManager.HasHotkey(item.Second) select item;

            foreach (var enabledHotkey in usedHotkeys)
            {
                Keys modifiers, key;

                Root.HotkeysManager.GetHotkey(enabledHotkey.Second, out modifiers, out key);
                enabledHotkey.First.Set(modifiers, key);
            }

            CheckHotkeys();
        }

        internal override void SaveOptions()
        {
            var modifiedHotkeys = from item in _hotkeys where item.First.Modified select item;

            foreach (var modifiedHotkey in modifiedHotkeys)
            {
                Keys modifiers, key;

                if (modifiedHotkey.First.Get(out modifiers, out key))
                {
                    Root.HotkeysManager.SetHotkey(modifiedHotkey.Second, modifiers, key);
                }
                else
                {
                    Root.HotkeysManager.RemoveHotkey(modifiedHotkey.Second);
                }
            }
        }

        private void CheckHotkeys()
        {
            // hide all providers
            foreach (var errorProvider in _providers)
            {
                errorProvider.Value = false;
                errorProvider.Key.Clear();
            }

            // and set them again
            foreach (HotkeyEdit edit in grHotkeys.Controls.Cast<Control>().Where(x => x is HotkeyEdit))
            {
                HotkeyEdit localEdit = edit;
                if (grHotkeys.Controls.Cast<Control>().Where(x => x is HotkeyEdit && x != localEdit).Any(x => !String.IsNullOrEmpty(x.Text) && String.Compare(x.Text, localEdit.Text) == 0))
                {
                    if (!_providers.Any(x => x.Value == false))
                    {
                        _providers.Add(new Pair<ErrorProvider, bool>(new ErrorProvider(this), false));
                    }
                    _providers.Find(x => !x.Value).Key.SetError(edit, Resources.DuplicateHotkey);
                    _providers.Find(x => !x.Value).Value = true;
                }
            }
        }
    }
}
